import pyglet
from math import sin, cos, pi, sqrt

twopi = 2*pi

delta_x, delta_y = 80.0, 150.0
window_width, window_height  = 600, 400
update_time = 1 / 60.0
screen_limit = 30
color_r, color_g, color_b = 1.0, 1.0, 0
vertices_number = 20

window = pyglet.window.Window(window_width, window_height)
fps_display = pyglet.clock.ClockDisplay()

x, y = window.width / 2, window.height / 2
vx, vy = delta_x, delta_y

@window.event
def on_draw():
    window.clear()
    def circle_vertices():
        delta_angle = twopi / vertices_number
        angle = 0
        while angle < twopi:
            yield x + screen_limit * cos(angle)
            yield y + screen_limit * sin(angle)
            angle += delta_angle

    pyglet.gl.glColor3f(color_r, color_g, color_b)
    # vertex_order = range(vertices_number)
    pyglet.graphics.draw(vertices_number, pyglet.gl.GL_LINE_LOOP,
                         ('v2f', tuple(circle_vertices())))

    fps_display.draw()


def update(dt):
    global x,y, vx, vy
    x += vx*dt
    y += vy*dt

    if x + screen_limit > window.width:
        x = window.width - screen_limit
        vx = - vx

    if x - screen_limit < 0:
        x =  screen_limit
        vx = - vx

    if y + screen_limit > window.height:
        y = window.height - screen_limit
        vy = - vy

    if y - screen_limit < 0:
        y = screen_limit
        vy = - vy

pyglet.clock.schedule_interval(update, update_time)

pyglet.app.run()

